package com.um.jdy.platform.user.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.um.jdy.common.member.enums.CodeKey;
import com.um.jdy.platform.user.entity.dto.MobileDTO;
import com.um.jdy.platform.user.entity.dto.ResetPasswordDTO;
import com.um.jdy.platform.user.manager.MobileManager;
import com.um.springboot.starter.orm.entity.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;

/**
 * 手机号码方式-注册及登录
 *
 * @author cs
 * 日期 2023/5/23
 * 版权所有 广州优匠科技有限公司
 */
@Api(tags = "手机号")
@RestController
@RequestMapping("/api/v1/user/mobile")
@Validated
public class MobileController {

    @Autowired
    MobileManager mobileManager;

    @ApiOperation(value = "登录")
    @PostMapping("/signin")
    public Response<String> signin(@RequestBody MobileDTO mobileDTO) {
        return mobileManager.signin(mobileDTO);
    }

    @ApiOperation(value = "发送验证码")
    @GetMapping("/send_code")
    public Response sendCode(@ApiParam(value = "手机号") @NotEmpty String mobile,
                             @ApiParam(value = "验证码标识") @NotEmpty String key) {
        CodeKey codeKey = CodeKey.get(key);
        mobileManager.sendCode(codeKey, mobile);
        return Response.success();
    }

    @ApiOperation(value = "验证原手机号")
    @PostMapping("/verify")
    @SaCheckLogin
    public Response verify(@RequestBody MobileDTO mobileDTO) {
        mobileManager.verify(mobileDTO);
        return Response.success();
    }

    @ApiOperation(value = "绑定新手机号")
    @PostMapping("/bind")
    @SaCheckLogin
    public Response bind(@RequestBody MobileDTO mobileDTO) {
        return mobileManager.bind(mobileDTO) ? Response.success() : Response.failed();
    }

    /**
     * 重置密码
     *
     * @return
     */
    @PostMapping("/reset_password")
    public Response resetPassword(@RequestBody ResetPasswordDTO resetPasswordDTO) {
        boolean result = mobileManager.resetPassword(resetPasswordDTO);
        return result ? Response.success() : Response.failed();
    }
}
